package br.ufc.si.sd.conexao;


import java.net.*;
import java.io.*;

public class Conexao {
  
    public byte[] sendMessage(byte[] message) {
        byte[] data=null;
        String ip = "localhost";
        Socket s = null;
   
        try {
            int serverPort = 5000;
            s = new Socket(ip, serverPort);
            DataInputStream in = new DataInputStream( s.getInputStream());
            DataOutputStream out = new DataOutputStream(s.getOutputStream());
            out.write(message);
            while(in.available()==0){};
            data = new byte[in.available()];
            in.read(data);
            
        } catch (UnknownHostException e) {
            System.out.println("Sock:" + e.getMessage());
        } catch (EOFException e) {
            System.out.println("EOF:" + e.getMessage());
        } catch (IOException e) {
            System.out.println("IO:" + e.getMessage());
        } finally {
            if (s != null) {
                try {
                    s.close();
                } catch (IOException e) {
                    System.out.println("close:" + e.getMessage());
                }
            }
        }
        
        return data;
    }
    
}